//由于三级列表和展示有共通数据，书写pinia管理数据
import { defineStore } from "pinia";
//引入类型和请求
import { requestCategory1, requestCategory2, requestCategory3 } from '@/api/attr'
import type {reqCategoryType} from '@/api/attr'
import { ElMessage } from "element-plus";

//创建小仓库
//定义数据类型
interface AttrStoreType{
    categoryList1:reqCategoryType,
    categoryList2: reqCategoryType,
    categoryList3: reqCategoryType,
    category1Id:number|null,
    category2Id:number|null,
    category3Id:number|null,
}
const useAttrStore = defineStore('attr', {
    //定义管理参数
    state: ():AttrStoreType => {
       return {
        categoryList1: [],
        categoryList2: [],
        categoryList3: [],
        category1Id: null,
        category2Id: null,
        category3Id:null 
       }
    },
    actions: { 
        //书写请求一级类型函数
       async getCateGoryList1(){
       try{
        //发请求
        const re = await requestCategory1()
        // console.log(re,'11');
        
        this.categoryList1 = re
       }catch(e){
        ElMessage.error('请求失败')
       }
    },

    //书写请求二级类型函数
    async getCateGoryList2(){
        try{
         //发请求
         const re = await requestCategory2(this.category1Id as number)
         this.categoryList2 = re
        }catch(e){
         ElMessage.error('请求二级列表失败')
        }
     },
     //书写请求三级类型函数
    async getCateGoryList3(){
        try{
         //发请求
         const re = await requestCategory3(this.category2Id as number)
         this.categoryList3 = re
        }catch(e){
         ElMessage.error('请求二级列表失败')
        }
     }
   }

})


export default useAttrStore;